package com.bw.controller.linkman;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.linkman.PiLinkman;
import com.bw.resp.ResponseResult;
import com.bw.service.linkman.IPiLinkmanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouxin
 * @since 2020-06-16
 */
@CrossOrigin
@RestController
@RequestMapping("linkman")
public class PiLinkmanController {

    @Autowired
    private IPiLinkmanService piLinkmanService;

    /*
    * 列表查询
    * */
    @PostMapping("list/{pagenum}/{pagesize}")
    public ResponseResult list(@RequestBody PiLinkman piLinkman,@PathVariable("pagenum") Integer currentPage,@PathVariable("pagesize") Integer pageSize){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            PageHelper.startPage(currentPage,pageSize);
            if (piLinkman.getName()!=null){
               queryWrapper.like("name",piLinkman.getName());
            }
            List<PiLinkman> list = piLinkmanService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setMessage("查询成功!");
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setMessage("查询失败!"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("addLinkMan")
    public ResponseResult addLinkMan(@RequestBody PiLinkman linkman){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            piLinkmanService.saveOrUpdate(linkman);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("保存失败!"+e.toString());
        }
        return result;
    }

    @PostMapping("LookDetail/{id}")
    public ResponseResult LookDetail(@PathVariable("id") String id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiLinkman piLinkman = piLinkmanService.getById(id);
            result.setResult(piLinkman);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败!"+e.toString());
        }
        return result;
    }

    @DeleteMapping("deleteLinkMan/{id}")
    public ResponseResult deleteLinkMan(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiLinkman piLinkman = piLinkmanService.getById(id);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("删除失败!"+e.toString());
        }
        return result;
    }


}
